//
//  DoctorDetailRegistrationView.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/4/20.
//  Copyright © 2020 QDSG. All rights reserved.
//

import UIKit

protocol DoctorDetailRegistrationViewDelagate: NSObjectProtocol {
    func doctorDetailRegistrationViewDidSelectedRow(indexPath: IndexPath, scheduleVm: DoctorScheduleViewModel, registrationListVm: CloudReserveDateListViewModel)
}

class DoctorDetailRegistrationView: UIView {
    
    weak var delegate: DoctorDetailRegistrationViewDelagate?
    var scheduleViewModel: DoctorScheduleViewModel! {
        didSet {
            setUI()
        }
    }
    var doctorViewModel: DoctorDetailViewModel!
    
    var registrationViewModel: CloudReserveDateViewModel? {
        didSet {
            tableView.reloadData()
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.alpha = 0
        mainWindow.addSubview(self)
    }
    
    func updateData(scheduleVm: DoctorScheduleViewModel, doctorVm: DoctorDetailViewModel) {
        scheduleViewModel = scheduleVm
        doctorViewModel = doctorVm
        loadData()
    }
    
    func setUI() {
        tableView.tableFooterView = UIView()
        tableView.rowHeight = 55
        tableView.separatorStyle = .none
        tableView.register(DoctorDetailRegistrationViewCell.nibForCell(),
                           forCellReuseIdentifier: DoctorDetailRegistrationViewCell.reuseIdentifier())
    }
    
    func show() {
        UIView.animate(withDuration: 0.24, animations: {
            self.alpha = 1
        }) { (finish) in
            
        }
    }
    
    func dismiss() {
        UIView.animate(withDuration: 0.24, animations: {
            self.alpha = 0
        }) { (finish) in
            if finish {
                self.removeFromSuperview()
            }
        }
    }

    @IBAction func closeBtnAction(_ sender: Any) {
        dismiss()
    }
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var bgView: UIView! {
        didSet {
            let corner: UIRectCorner = [.topLeft, .topRight]
            bgView.layerCorners(rect: bgView.frame, radius: 26, corners: corner)
        }
    }
    
}

extension DoctorDetailRegistrationView: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return registrationViewModel?.numberOfItems ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: DoctorDetailRegistrationViewCell.reuseIdentifier(), for: indexPath) as! DoctorDetailRegistrationViewCell
        if let vm = registrationViewModel?.viewModelForRegistration(indexPath: indexPath) {
            cell.updateData(vm: vm)
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: screenW, height: 44))
        view.backgroundColor = .init(hex: "#FEF8EB")
        let imgView = UIImageView(image: UIImage(named: "registration_view_clock"))
        let label = UILabel()
        label.tag = 100
        label.font = .medium(15)
        label.textColor = .init(hex: "#E1B26E")
        label.text = scheduleViewModel?.scheduleTime
        view.addSubview(imgView)
        view.addSubview(label)
        imgView.snp.makeConstraints { (make) in
            make.left.equalTo(16)
            make.width.height.equalTo(18)
            make.centerY.equalToSuperview()
        }
        label.snp.makeConstraints { (make) in
            make.left.equalTo(imgView.snp.right).offset(8)
            make.centerY.equalTo(imgView.snp.centerY)
            make.right.equalTo(-16)
        }
        return view
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 44
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let vm = registrationViewModel?.viewModelForRegistration(indexPath: indexPath) {
            if vm.isEnabled {
                delegate?.doctorDetailRegistrationViewDidSelectedRow(indexPath: indexPath, scheduleVm: scheduleViewModel, registrationListVm: vm)
                dismiss()
            } else {
                HUDTool.show(message: "当前号源不可预约")
            }

        }
    }
}

extension DoctorDetailRegistrationView {
    func loadData() {
        CloudReserveDateViewModel.getReserveDateList(doctorId: doctorViewModel.doctorId,
                                                     departmentId: scheduleViewModel.departmentId,
                                                     scheduleDate: scheduleViewModel.scheduleDate,
                                                     timeInterval: scheduleViewModel.timeInterval,
                                                     businessType: "3", success: { (model) in
            self.registrationViewModel = CloudReserveDateViewModel(model: model)
        }) { (errorMsg) in
            HUDTool.showError(message: errorMsg)
        }
    }
}
